package com.qujia.station.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.qujia.common.core.domain.entity.SysUser;
import com.qujia.common.exception.CustomException;
import com.qujia.common.utils.SecurityUtils;
import com.qujia.station.domain.Oil;
import com.qujia.station.domain.vo.OilListVo;
import com.qujia.station.mapper.OilMapper;
import com.qujia.station.service.IOilService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 油站信息Service业务层处理
 * 
 * @author qujia
 * @date 2020-12-08
 */
@Service
public class OilServiceImpl implements IOilService
{
    @Autowired
    private OilMapper oilMapper;

    /**
     * 查询油品信息
     *
     * @param oilId 油品信息ID
     * @return 油品信息
     */
    public Oil selectOilById(Long oilId){
        return  oilMapper.selectOilById(oilId);
    }

    /**
     * 查询油品信息列表
     *
     * @param oil 油品信息
     * @return 油品信息集合
     */
    public List<Oil> selectOilList(Oil oil){
        return  oilMapper.selectOilList(oil);
    }

    /**
     * 新增油品信息
     *
     * @param oil 油品信息
     * @return 结果
     */
    public int insertOil(Oil oil){
        return  oilMapper.insertOil(oil);
    }

    /**
     * 修改油站信息
     *
     * @param oil 油企信息
     * @return 结果
     */
    public int updateOil(Oil oil){
        return  oilMapper.updateOil(oil);
    }


    /**
     * 删除油品信息
     *
     * @param oilId 油品信息ID
     * @return 结果
     */
    public int deleteOilById(Long oilId){
        return  oilMapper.deleteOilById(oilId);
    }

    /**
     * 批量删除油品信息
     *
     * @param oilIds 需要删除的数据ID
     * @return 结果
     */
    public int deleteOilByIds(Long[] oilIds){
        return  oilMapper.deleteOilByIds(oilIds);
    }

    @Override
    public List<OilListVo> getListByEnterprise() {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        if (ObjectUtils.notEqual(sysUser.getUserLevel(),  "1")) throw new CustomException("使用油企账号登录！");

        List<OilListVo> oilListVoList = new ArrayList<>();
        List<Oil> oilList = oilMapper.getListByEnterprise(sysUser.getEnterpriseId());
        if (ObjectUtils.isEmpty(oilList)) return oilListVoList;

        oilList.forEach(oil -> {
            OilListVo oilListVo = new OilListVo();
            oilListVo.setOilId(oil.getOilId());
            oilListVo.setOilName(oil.getOilName() + oil.getOilClass());
            oilListVoList.add(oilListVo);
        });

        return oilListVoList;
    }

    @Override
    public List<OilListVo> getListByEnterpriseOrStationId(Long stationId) {
        List<OilListVo> oilListVoList = new ArrayList<>();
        //如果传入油站id则只查单个油站的油品
        if (null != stationId){
            List<Oil> listByStationId = oilMapper.getListByStationId(stationId);
            if (CollectionUtil.isEmpty(listByStationId)) return oilListVoList;

            listByStationId.forEach(oil -> {
                OilListVo oilListVo = new OilListVo();
                oilListVo.setOilId(oil.getOilId());
                oilListVo.setOilName(oil.getOilName() + oil.getOilClass());
                oilListVo.setOilPrice(oil.getOilStationPrice());
                oilListVoList.add(oilListVo);
            });

            return oilListVoList;
        }

        //否则查改油企所有的油品
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        if (ObjectUtils.notEqual(sysUser.getUserLevel(),  "1")) throw new CustomException("使用油企账号登录！");


        List<Oil> oilList = oilMapper.getListByEnterprise(sysUser.getEnterpriseId());
        if (ObjectUtils.isEmpty(oilList)) return oilListVoList;

        oilList.forEach(oil -> {
            OilListVo oilListVo = new OilListVo();
            oilListVo.setOilId(oil.getOilId());
            oilListVo.setOilName(oil.getOilName() + oil.getOilClass());
//            oilListVo.setOilPrice(oil.getOilStationPrice());
            oilListVoList.add(oilListVo);
        });

        return oilListVoList;
    }

    @Override
    public List<Oil> getListByStationId(Long stationId) {
        return  oilMapper.getListByStationId(stationId);
    }

}
